# -*- coding: utf-8 -*-

'''
    【简介】
	PySide6中主窗口例子
'''


import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from PySide6 import QtCore
from PySide6.QtGui import QIcon, QGuiApplication

import os
# 改变当前工作目录到当前文件所在的目录
os.chdir(os.path.dirname(__file__))

# 定义一个MainWindow类，继承自QMainWindow
class MainWindow(QMainWindow):
    # 初始化函数，parent参数可选
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        # 设置窗口标题
        self.setWindowTitle("QMainWindow 例子")
        # 设置窗口大小
        self.resize(800, 800)
        # 获取状态栏
        self.status = self.statusBar()
        # 显示一条消息，延时1000毫秒
        self.status.showMessage("启动中", 1000)
        
        # 创建一个垂直布局
        layout = QVBoxLayout()
        # 创建一个widget，并设置布局
        widget = QWidget(self)
        widget.setLayout(layout)
        # 设置widget的位置和大小
        widget.setGeometry(QtCore.QRect(200, 50, 200, 400))
        #self.setCentralWidget(widget)
        # 将widget赋值给MainWindow的widget属性
        self.widget = widget
        
        # 添加按钮1，点击关闭主窗口
        self.button1 = QPushButton("关闭主窗口")
        self.button1.clicked.connect(self.close)
        layout.addWidget(self.button1)
        
        # 添加按钮2，点击将主窗口居中
        self.button2 = QPushButton("主窗口居中")
        self.button2.clicked.connect(self.center)
        layout.addWidget(self.button2)
        

        self.button3 = QPushButton("显示图标")
        self.button3.clicked.connect(lambda :self.setWindowIcon(QIcon("./images/cartoon1.ico")))
        layout.addWidget(self.button3)
        

        self.button4 = QPushButton("显示状态栏")
        self.button4.clicked.connect(lambda :self.status.showMessage("显示状态栏",5000))
        layout.addWidget(self.button4)
        
        self.button5 = QPushButton("显示窗口坐标及大小")
        self.button5.clicked.connect(self.show_geometry)
        layout.addWidget(self.button5)
        
        self.button6 = QPushButton("widget移动到0,0")
        self.button6.clicked.connect(lambda :self.widget.move(0,0))
        layout.addWidget(self.button6)
        
        self.button7 = QPushButton("widget宽300,高300")
        self.button7.clicked.connect(lambda :self.widget.resize(300,300))
        layout.addWidget(self.button7)
        
        self.button8 = QPushButton("设置几何数据（x,y,w,h）(0,0,500,500)")
        self.button8.clicked.connect(lambda :self.widget.setGeometry(0,0,500,500))
        layout.addWidget(self.button8)
        
        #最大和最小一样就是固定尺寸
        # self.setMinimumSize(800,800)
        # self.setMaximumSize(800,800)
        
        #固定尺寸
        #self.setFixedSize(self.size())
        
        #宽高自适应
        self.widget.adjustSize()
        
    def show_geometry(self):
        print('主窗口坐标信息，相对于屏幕')
        print('主窗口最小尺寸',self.minimumSize().toTuple())
        print('主窗口：宽',self.size().width(),'主窗口：y',self.pos().y())
        print(
            f'主窗口: x={self.x()}, y={self.y()}, width={self.width()}, height={self.height()}：'
        )
        print(
            f'主窗口geometry: x={self.geometry().x()}, y={self.geometry().y()}, width={self.geometry().width()}, height={self.geometry().height()}：'
        )
        print(
            f'主窗口frameGeometry: x={self.frameGeometry().x()}, y={self.frameGeometry().y()}, width={self.frameGeometry().width()}, height={self.frameGeometry().height()}：'
        )

        print('\n子窗口QWidget坐标信息，相对于主窗口：')
        print(
            f'子窗口self.widget: x={self.widget.x()}, y={self.widget.y()}, width={self.widget.width()}, height={self.widget.height()}：'
        )
        print(
            f'子窗口self.widget.geometry: x={self.widget.geometry().x()}, y={self.widget.geometry().y()}, width={self.widget.geometry().width()}, height={self.widget.geometry().height()}：'
        )
        print(
            f'子窗口self.widget.frameGeometry: x={self.widget.frameGeometry().x()}, y={self.widget.frameGeometry().y()}, width={self.widget.frameGeometry().width()}, height={self.widget.frameGeometry().height()}：'
        )
    def center(self):
        # 获取屏幕尺寸
        screen = QGuiApplication.primaryScreen().geometry()
        # 获取窗口尺寸
        size = self.geometry()
        # 将窗口居中
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec())
